package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.dto.DeviceAlertDto;
import com.csun.cmny.provider.model.dto.OldmanRpcDto;
import com.csun.cmny.provider.model.dto.PointCardAccountDto;
import com.csun.cmny.provider.model.dto.PointCardAccountQueryDto;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupOldmanVo;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupRoomVo;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupUserVo;
import com.csun.cmny.provider.service.OmcOldmanFeignApi;
import com.csun.cmny.provider.service.OmcRoomFeignApi;
import com.csun.cmny.provider.service.OmcRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-22 14:25
 * @Description: the class omc rpc service impl
 */
@Slf4j
@Service
public class OmcRpcServiceImpl implements OmcRpcService {

    @Resource
    private OmcOldmanFeignApi omcOldmanFeignApi;

    @Resource
    private OmcRoomFeignApi omcRoomFeignApi;

    /**
     * get oldman by oldman id
     *
     * @param oldmanRpcDto oldman rpc dto
     * @return the oldman rpc vo
     */
    @Override
    public OldmanRpcVo getOldmanByOldmanId(OldmanRpcDto oldmanRpcDto) {

        try {
            Wrapper<OldmanRpcVo> wrapper = omcOldmanFeignApi.checkOldmanbelong(oldmanRpcDto);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031017, oldmanRpcDto.getOldmanId());
            }

            OldmanRpcVo result = wrapper.getResult();

            assert result != null;
            return result;
        } catch (Exception e) {
            log.error("getOldmanByOldmanId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query room building by room id
     *
     * @param roomId    room id
     *
     * @return          the room building bo
     */
    @Override
    public RoomBuildingVo queryRoomBuildingByRoomId(Integer roomId) {

        try {
            Wrapper<RoomBuildingVo> wrapper = omcRoomFeignApi.queryRoomBuildingByRoomId(roomId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610059);
            }

            RoomBuildingVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getOldmanByOldmanId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query oldman bed room building by oldman id
     *
     * @param oldmanId oldman id
     * @return oldman bed room building vo
     */
    @Override
    public OldmanBedRoomBuildingVo queryOldmanBedRoomBuildingByOldmanId(Integer oldmanId) {

        try {
            Wrapper<OldmanBedRoomBuildingVo> wrapper = omcOldmanFeignApi.queryOldmanBedRoomBuildingByOldmanId(oldmanId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610063);
            }

            OldmanBedRoomBuildingVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getOldmanByOldmanId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query oldman bed room building by bed id
     *
     * @param bedId bed id
     * @return oldman bed room building vo
     */
    @Override
    public OldmanBedRoomBuildingVo queryOldmanBedRoomBuildingByBedId(Integer bedId) {

        try {
            Wrapper<OldmanBedRoomBuildingVo> wrapper = omcOldmanFeignApi.queryOldmanBedRoomBuildingByBedId(bedId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610061);
            }

            OldmanBedRoomBuildingVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getOldmanByOldmanId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query oldman special case by oldman id
     *
     * @param oldmanId the oldman id
     *
     * @return the oldman special case
     */
    @Override
    public String queryOldmanSpecialCaseByOldmanId(Integer oldmanId) {

        try {
            Wrapper<String> wrapper = omcOldmanFeignApi.queryOldmanSpecialCaseByOldmanId(oldmanId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610061);
            }

            String result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryOldmanSpecialCaseByOldmanId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query nursing group user by user id
     *
     * @param userId the user id
     * @return the nursing group user vo
     */
    @Override
    public NursingGroupUserVo queryNursingGroupUserByUserId(Long userId) {

        try {
            Wrapper<NursingGroupUserVo> wrapper = omcOldmanFeignApi.queryNursingGroupUserByUserId(userId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610061);
            }

            NursingGroupUserVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryNursingGroupUserByUserId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query nursing group room by room id
     *
     * @param roomId the room id
     * @return the nursing group room vo
     */
    @Override
    public NursingGroupRoomVo queryNursingGroupRoomByRoomId(Integer roomId) {

        try {
            Wrapper<NursingGroupRoomVo> wrapper = omcOldmanFeignApi.queryNursingGroupRoomByRoomId(roomId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610061);
            }

            NursingGroupRoomVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryNursingGroupRoomByRoomId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query nursing group oldman vo
     *
     * @param oldmanId the oldman id
     *
     * @return the nursing group oldman vo
     */
    @Override
    public NursingGroupOldmanVo queryNursingGroupOldmanByOldmanId(Integer oldmanId) {

        try {
            Wrapper<NursingGroupOldmanVo> wrapper = omcOldmanFeignApi.queryNursingGroupOldmanByOldmanId(oldmanId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610061);
            }

            NursingGroupOldmanVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryNursingGroupRoomByRoomId={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public Integer syncAlertMsgToShiftLog(DeviceAlertDto dto) {
        try {
            Wrapper<Integer> wrapper = omcOldmanFeignApi.syncAlertMsgToShiftLog(dto);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610043);
            }

            return wrapper.getResult();
        } catch (Exception e) {
            log.error("syncAlertMsgToShiftLog={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public Long selectOldByRoomId(Long roomId) {
        try {
            Wrapper<Long> wrapper = omcOldmanFeignApi.selectOldByRoomId(roomId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610043);
            }

            return wrapper.getResult();
        } catch (Exception e) {
            log.error("roomId={}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public List<LifeRoomBuildingVo> listRoomByGroupId(Long groupId) {
        try {
            Wrapper<List<LifeRoomBuildingVo>> wrapper = omcRoomFeignApi.listRoomByGroupId(groupId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610043);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("groupId = {}", groupId);
        }
        return null;
    }

    @Override
    public List<PointCardAccountVO> listPointCardAccount(PointCardAccountQueryDto pointCardAccountQueryDto) {
        try {
            Wrapper<List<PointCardAccountVO>> wrapper = omcOldmanFeignApi.listPointCardAccount(pointCardAccountQueryDto);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.GL99990500);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("listPointCardAccount = {}", e.getMessage());
        }
        return null;
    }

    @Override
    public Integer addPointCardAccount(PointCardAccountDto dto) {
        try {
            Wrapper<Integer> wrapper = omcOldmanFeignApi.addPointCardAccount(dto);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.GL99990500);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("listPointCardAccount = {}", e.getMessage());
        }
        return 0;
    }

    @Override
    public Integer deletePointCardAccount(Long deviceId) {
        try {
            Wrapper<Integer> wrapper = omcOldmanFeignApi.deletePointCardAccount(deviceId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.GL99990500);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("deletePointCardAccount = {}", e.getMessage());
        }
        return 0;
    }

    @Override
    public List<Integer> listBedByRoomId(Integer roomId) {
        try {
            Wrapper<List<Integer>> wrapper = omcRoomFeignApi.listBedByRoomId(roomId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.GL99990500);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("deletePointCardAccount = {}", e.getMessage());
        }
        return null;
    }


}
